﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevExpress.Web.ASPxEditors;
using DevExpress.Web.ASPxGridView;
using OTS.BLL;
using OTS.Model;

public partial class OLPC_MCInfomationM : System.Web.UI.Page
{
    const string PageSizeSessionKey = "ed5e843d-cff7-47a7-815e-832923f7fb09";
    private List<DataTable> listdt = new List<DataTable>();
    private DataTable itemdt = new DataTable();
    private DataTable importeddt = new DataTable();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            CommonMethod.IsTimeOutUserCode(this, CommonModel.errorTimeOutUrl1);
            SettingRight();
            Session["listdt"] = null;
            Session["dtlist"] = null;
            //txtBankReference.Text = "334543.98-".LastIndexOf('-').ToString();
            //txtOrderingPartyBankName.Text = "-43242.33".LastIndexOf('-').ToString();
            //txtCustomerReference.Text = double.Parse("343,343.00").ToString("###,###.##");
            //txtEntryDate.Text = float.Parse("343,4343.343-").ToString();
            bindComany();
            InitDataBind();
        }
        //InitDataBind();
        //TemplateDataBind();
    }
    /// <summary>
    /// Page.VerifyRenderingInServerForm 方法确认在运行时为指定的 ASP.NET 服务器控件呈现 HTMLForm 控件
    /// </summary>
    /// <param name="control"></param>
    //public override void VerifyRenderingInServerForm(Control control)
    //{
    //    //base.VerifyRenderingInServerForm(control);
    //}

    // 页面权限
    private void SettingRight()
    {
        bool[] rightlist = SysRightBLL.GetRightByType(new string[] { 
            Session["UserCode"].ToString(), 
            Session["CoCode"].ToString(), 
            "mcinformationm"});
        if (!rightlist[0])
        { this.Page.Response.Write(String.Format("<script language='javascript'>window.parent.location.href='{0}';</script>", CommonModel.loginOut1)); }

        tc.TabPages[1].Visible = rightlist[1];
        gvimported.Columns[0].Visible = rightlist[2];
        gvimported.Columns[1].Visible = rightlist[3];
        tc.TabPages[2].Visible = rightlist[1] ? true : (rightlist[3] ? true : false);
    }

    // 初始化绑定
    //绑定公司
    public void bindComany()
    {
        DataTable dt = AccountStatementItemBLL.GetUserCompanyList(Session["UserCode"].ToString());
        if (dt != null && dt.Rows.Count > 0)
        {
            ddlCompany.DataSource = dt;
            ddlCompany.ValueField = "CoCode";
            ddlCompany.TextField = "CoName";
            ddlCompany.DataBind();
            ddlcompanyimport.DataSource = dt;
            ddlcompanyimport.ValueField = "CoCode";
            ddlcompanyimport.TextField = "CoName";
            ddlcompanyimport.DataBind();
            cbcompany.DataSource = dt;
            cbcompany.ValueField = "CoCode";
            cbcompany.TextField = "CoName";
            cbcompany.DataBind();
            //ddlCompany.Items.Add("请选择 (Please Select)", "-1");
            //ddlcompanyimport.Items.Add("请选择 (Please Select)", "-1");
            ddlcompanyimport.SelectedIndex = 0;
            ddlCompany.SelectedIndex = 0;
            cbcompany.SelectedIndex = 0;
        }
    }
    // 已有数据
    private void InitDataBind()
    {
        gvimported.DataSource = null;
        importeddt = new DataTable();
        if (Session["dtlist"] != null)
        {
            importeddt = Session["dtlist"] as DataTable;
        }
        else if (cbcompany.Items.Count > 0)
        {
            importeddt = AccountStatementItemBLL.GetList(string.Format(" CoCode='{0}' ", cbcompany.SelectedItem.Value));
        }
        gvimported.DataSource = importeddt;
        gvimported.DataBind();
        Session["dtlist"] = importeddt;
    }
    // 数据查询、排序
    protected void gv_AfterPerformCallback(object sender,
        DevExpress.Web.ASPxGridView.ASPxGridViewAfterPerformCallbackEventArgs e)
    {
        InitDataBind();
    }
    
    // 模版数据绑定
    private void TemplateDataBind()
    {
        itemdt = new DataTable();
        if (Session["listdt"] != null)
        {
            listdt = Session["listdt"] as List<DataTable>;
            if (listdt != null && listdt.Count > 2 && listdt[2] != null)
            {
                itemdt = listdt[2];
            }
        }
        else
        {
            if (fileload != null)
            {
                try
                {
                    string filepath = fileload.PostedFile.FileName;
                    hidfirstflag.Value = "1";
                    if (filepath != null && filepath.Length > 0)
                    {
                        string filename = filepath.Substring(filepath.LastIndexOf("\\") + 1);
                        string lastname = filename.Substring(filename.LastIndexOf(".") + 1);
                        if (lastname.ToLower().Equals("xls"))
                        {
                            if (ddlcompanyimport == null || ddlcompanyimport.Value.Equals("-1"))
                            {
                                CommonMethod.regAlertMessage(udp, "公司不能为空，请选择！", this);
                                ddlCompany.Focus();
                                return;
                            }
                            fileload.SaveAs(Server.MapPath("~/TempFiles/" + filename), true);
                            string cocode = ddlcompanyimport.SelectedItem.Value.ToString();
                            string companyname = ddlcompanyimport.SelectedItem.Text;
                            listdt = MCInfomationMBLL.GetTemplateListDt(Server.MapPath("~/TempFiles/" + filename), cocode, companyname);
                            Session["listdt"] = listdt;
                            if (listdt != null && listdt.Count > 2 && listdt[2] != null)
                            {
                                itemdt = listdt[2];
                                //gvreadyforimport.DataSource = itemdt;
                                //gvreadyforimport.DataBind();
                            }
                        }
                        else
                        {
                            CommonMethod.regAlertMessage(udp, "模版文件格式不是有效的*.xls", this);
                        }
                    }
                    else
                    {
                        CommonMethod.regAlertMessage(udp, "模版文件无效！", this);
                    }
                }
                catch (Exception ex)
                {
                    if (hidfirstflag.Value == "1")
                    {
                        CommonMethod.regAlertMessage(udp, "加载模版数据出错，请重新选择文件，并确认文件路径是否正确！", this);
                        Log.WritLog("btnload_Click:", ex.ToString());
                    }
                }
            }
        }
        gvreadyforimport.DataSource = itemdt;
        gvreadyforimport.DataBind();
    }
    // 加载模版数据
    protected void btnload_Click(object sender, EventArgs e)
    {
        Session["listdt"] = null;
        TemplateDataBind();
    }
    // 排序、查询
    protected void gvreadyforimport_AfterPerformCallback(object sender, 
        DevExpress.Web.ASPxGridView.ASPxGridViewAfterPerformCallbackEventArgs e)
    {
        TemplateDataBind();
    }

    //导入删除
    protected void gvreadyforimport_RowCommand(object sender, ASPxGridViewRowCommandEventArgs e)
    {
        try
        {
            string temp = e.CommandArgs.CommandArgument.ToString();
            int Inttemp = int.Parse(e.KeyValue.ToString());
            string cmd = e.CommandArgs.CommandName;

            if (Session["listdt"] != null)
            {
                listdt = Session["listdt"] as List<DataTable>;
                if (listdt != null && listdt.Count > 2 && listdt[2] != null)
                {
                    DataTable tempdt = listdt[2];
                    if (tempdt != null && tempdt.Rows.Count >= Inttemp)
                    {
                        if (cmd == "delete")
                            tempdt.Rows.RemoveAt(Inttemp - 1);
                        else if (cmd == "handlingfee")
                        {
                            CheckBox chk = sender as CheckBox;
                            tempdt.Rows[Inttemp - 1]["HandlingFee"] = chk.Checked ? 1 : 0;
                        }
                        else if (cmd == "accepted")
                        {
                            CheckBox chk = sender as CheckBox;
                            tempdt.Rows[Inttemp - 1]["Accepted"] = chk.Checked ? 1 : 0;
                        }
                        tempdt = CommonBLL.AddColumn(tempdt, "rowindex");
                        listdt[2] = tempdt;
                        Session["listdt"] = listdt;
                        gvreadyforimport.DataSource = null;
                        TemplateDataBind();
                    }
                }
            }
            else
            {
                gvreadyforimport.DataSource = null;
                gvreadyforimport.DataBind();
                CommonMethod.regAlertMessage(udp, "没有数据！", this);
            }
        }
        catch (Exception ex)
        {
            Log.WritLog("gvreadyforimport_RowCommand:", ex.ToString());
        }
    }
    // 模版数据_添加
    protected void btnImportAdd_Click(object sender, EventArgs e)
    {
        CommonMethod.IsTimeOutUserCode(this);
        if (Session["listdt"] != null)
        {
            listdt = Session["listdt"] as List<DataTable>;

            if (listdt != null && listdt.Count > 2 && listdt[2] != null && listdt[2].Rows.Count > 0)
            {
                string usercode = Session["UserCode"] as string;
                int resultInt = MCInfomationMBLL.Add(listdt, usercode);
                CommonMethod.regAlertMessage(udp, string.Format("已经成功导入：{0} 笔记录", resultInt), this);
                CommonMethod.regAlertMessage(udp, "SetConvertDiv('none');", this);
                itemdt = new DataTable();
                Session["listdt"] = null;
                gvreadyforimport.DataSource = null;
                Session["dtlist"] = null;
                InitDataBind();
                tc.ActiveTabIndex = 0;
            }
            else
                CommonMethod.regAlertMessage(udp, "没有相关行！", this);
        }
        else
        {
            TemplateDataBind();
            CommonMethod.regAlertMessage(udp, "获取相关数据超时，请重新登陆！", this);
        }
    }
    // 检查是否符合要求
    private bool Check()
    {
        if (ddlCompany == null || ddlCompany.Value.Equals("-1"))
        {
            CommonMethod.regAlertMessage(udp, "公司不能为空，请选择！", this);
            ddlCompany.Focus();
            return false;
        }
        if (string.IsNullOrEmpty(txtBankReference.Text.Trim()))
        {
            CommonMethod.regAlertMessage(udp, "银行参考号不能为空！", this);
            txtBankReference.Focus();
            return false;
        }
        if (string.IsNullOrEmpty(txtEntryDate.Text.Trim()))
        {
            CommonMethod.regAlertMessage(udp, "入账日期不能为空！", this);
            txtEntryDate.Focus();
            return false;
        }
        if (string.IsNullOrEmpty(txtTransactionAmount.Text.Trim()))
        {
            CommonMethod.regAlertMessage(udp, "交易金额不能为空！", this);
            txtTransactionAmount.Focus();
            return false;
        }
        if (!CommonMethod.CheckNUM(txtTransactionAmount.Text.Trim()))
        {
            CommonMethod.regAlertMessage(udp, "交易金额只能为数字！", this);
            txtTransactionAmount.Focus();
            return false;
        }
        if (string.IsNullOrEmpty(txtOrderingPartyName.Text.Trim()))
        {
            CommonMethod.regAlertMessage(udp, "汇款人/开票人名称不能为空！", this);
            txtOrderingPartyName.Focus();
            return false;
        }
        if (string.IsNullOrEmpty(txtOrderingPartyBankName.Text.Trim()))
        {
            CommonMethod.regAlertMessage(udp, "定购方银行名称不能为空！", this);
            txtOrderingPartyBankName.Focus();
            return false;
        }
        if (!string.IsNullOrEmpty(hidstatevalue.Value) && hidappliedamount.Value != "0")
        {
            if (float.Parse(hidappliedamount.Value) < float.Parse(txtTransactionAmount.Text.Trim()))
            {
                CommonMethod.regAlertMessage(udp,
                    string.Format("修改后的金额：{0} 不能小于已匹配的金额：{1} ", 
                    txtTransactionAmount.Text, hidappliedamount.Value), 
                    this);
                txtTransactionAmount.Focus();
                return false;
            }
        }

        return true;
    }
    // SetModel
    private AccountStatementItemModel SetItemModel(AccountStatementItemModel model)
    {
        #region Model
        string usercode = Session["UserCode"] as string;
        //string ascode = MCInfomationMBLL.GetAccountStatementCode();
        model.BankReference = txtBankReference.Text.Trim();
        model.CustomerReference = txtCustomerReference.Text.Trim();
        model.CustomerName = txtCustomerName.Text.Trim();
        model.CustomerCode = txtCustomerCode.Text.Trim();
        model.ValueDate = txtValueDate.Text.Trim();
        model.EntryDate = txtEntryDate.Text.Trim();
        model.TransactionAmount = txtTransactionAmount.Text.Trim();
        model.ProductType = txtProductType.Text.Trim();
        model.TransactionDescription = txtTransactionDescription.Text.Trim();
        model.ExtraInformation = txtExtraInformation.Text.Trim();
        model.OrderingPartyName = txtOrderingPartyName.Text.Trim();
        model.OrderingPartyAccount = txtOrderingPartyAccount.Text.Trim();
        model.OrderingPartyBankName = txtOrderingPartyBankName.Text.Trim();
        model.PaymentDetails = txtPaymentDetails.Text.Trim();
        model.MOLine = txtMOLine.Text.Trim();
        model.MOPerson = txtMOPerson.Text.Trim();
        model.MaturityDate = txtMaturityDate.Text.Trim();
        model.CoCode = ddlCompany.Value.ToString().Equals("-1") ? "" : ddlCompany.Value.ToString();
        model.StateValue = "import";
        model.StateText = "录入";
        model.Remarks = txtRemarks.Text.Trim();
        model.ADDAUTHOR = usercode;
        model.ADDDATE = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        model.UPDATEAUTHOR = usercode;
        model.UPDATEDATE = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        model.HandlingFee = btninptuadd.Visible ? 0 : int.Parse(hidHandlingFee.Value);
        model.Accepted = chkAcceted.Checked ? 1 : 0;
        model.Currency = btninptuadd.Visible ? "CNY" : hidCurrency.Value;
        model.AccountsReceivable = ddlAccountsReceivable.Text;

        #endregion

        return model;
    }
    // 删除、修改
    protected void gv_RowCommand(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewRowCommandEventArgs e)
    {
        try
        {
            string temp = e.CommandArgs.CommandArgument.ToString();
            int Inttemp = int.Parse(temp);
            string cmd = e.CommandArgs.CommandName;
            //CommonMethod.regAlertMessage(udp, Inttemp.ToString(), this);

            if (cmd == "delete")
            {
                #region

                bool flag = AccountStatementItemBLL.Delete(temp);
                if (flag)
                {
                    //CommonMethod.regAlertMessage(UpdatePanel, "删除成功！", this);
                    Session["dtlist"] = null;
                    InitDataBind();
                }
                else
                    CommonMethod.regAlertMessage(udp, "删除失败！", this);

                #endregion
            }
            else if (cmd == "edit")
            {
                #region
                //int rowindex = e.VisibleIndex;
                DataRow row;
                string strwhere = string.Format("AccountStatementItemCode='{0}'", e.KeyValue);
                if (Session["dtlist"] != null)
                {
                    DataTable tempdt = Session["dtlist"] as DataTable;
                    DataRow[] rows = tempdt.Select(strwhere);
                    row = rows[0];
                }
                else
                {
                    DataTable tempdt = AccountStatementItemBLL.GetList(strwhere);
                    DataRow[] rows = tempdt.Select(strwhere);
                    row = rows[0];
                }

                if (row != null && row["BankReference"] != null)
                {
                    Clear();
                    tc.ActiveTabIndex = 2;
                    btnedit.Visible = true;
                    btninptuadd.Visible = false;
                    string dataformat = "yyyy-MM-dd";

                    hidAccountStatementItemCode.Value = row["AccountStatementItemCode"] == null ? "" : row["AccountStatementItemCode"].ToString();
                    txtBankReference.Text = row["BankReference"] == null ? "" : row["BankReference"].ToString();
                    txtCustomerReference.Text = row["CustomerReference"] == null ? "" : row["CustomerReference"].ToString();
                    txtCustomerName.Text = row["CustomerName"] == null ? "" : row["CustomerName"].ToString();
                    txtCustomerCode.Text = row["CustomerCode"] == null ? "" : row["CustomerCode"].ToString();
                    txtValueDate.Text = row["ValueDate"] == null ? "" : CommonBLL.DateFormat(row["ValueDate"].ToString(), dataformat);
                    txtEntryDate.Text = row["EntryDate"] == null ? "" : CommonBLL.DateFormat(row["EntryDate"].ToString(), dataformat);
                    txtTransactionAmount.Text = row["TransactionAmount"] == null ? "" : row["TransactionAmount"].ToString();
                    txtTransactionDescription.Text = row["TransactionDescription"] == null ? "" : row["TransactionDescription"].ToString();
                    txtExtraInformation.Text = row["ExtraInformation"] == null ? "" : row["ExtraInformation"].ToString();
                    txtOrderingPartyName.Text = row["OrderingPartyName"] == null ? "" : row["OrderingPartyName"].ToString();
                    txtOrderingPartyAccount.Text = row["OrderingPartyAccount"] == null ? "" : row["OrderingPartyAccount"].ToString();
                    txtOrderingPartyBankName.Text = row["OrderingPartyBankName"] == null ? "" : row["OrderingPartyBankName"].ToString();
                    txtPaymentDetails.Text = row["PaymentDetails"] == null ? "" : row["PaymentDetails"].ToString();
                    txtMOLine.Text = row["MOLine"] == null ? "" : row["MOLine"].ToString();
                    txtMOPerson.Text = row["MOPerson"] == null ? "" : row["MOPerson"].ToString();
                    txtMaturityDate.Text = row["MaturityDate"] == null ? "" : CommonBLL.DateFormat(row["MaturityDate"].ToString(), dataformat);
                    txtRemarks.Text = row["Remarks"] == null ? "" : row["Remarks"].ToString();
                    hidstatevalue.Value = row["StateValue"].ToString();
                    hidappliedamount.Value = row["AppliedAmount"].ToString() ?? "0";
                    //chkAcceted.Checked = "1".Equals(row["Acceted"].ToString());
                    if (ddlCompany != null && ddlCompany.Items.Count > 0 && row["CoCode"] != null)
                    {
                        ddlCompany.SelectedIndex = 0;
                        for (int i = 0; i < ddlCompany.Items.Count; i++)
                        {
                            if (ddlCompany.Items[i].Value.Equals(row["CoCode"].ToString()))
                            {
                                ddlCompany.SelectedIndex = i;
                                break;
                            }
                        }
                    } if (ddlAccountsReceivable != null && ddlAccountsReceivable.Items.Count > 0 && row["AccountsReceivable"] != null)
                    {
                        ddlAccountsReceivable.SelectedIndex = 0;
                        for (int i = 0; i < ddlAccountsReceivable.Items.Count; i++)
                        {
                            if (ddlAccountsReceivable.Items[i].Text.Equals(row["AccountsReceivable"].ToString()))
                            {
                                ddlAccountsReceivable.SelectedIndex = i;
                                break;
                            }
                        }
                    }
                }
                #endregion
            }
            else if (cmd == "handlingfee" || cmd == "accepted")
            {
                CheckBox chk = sender as CheckBox;
                int f = chk.Checked ? 1 : 0;
                bool flag = AccountStatementItemBLL.Update(temp, cmd, f);
                if (flag)
                {
                    CommonMethod.regAlertMessage(udp, "设置成功！", this);
                    Session["dtlist"] = null;
                    InitDataBind();
                }
                else
                {
                    CommonMethod.regAlertMessage(udp, "设置失败！", this);
                }
            }
        }
        catch (Exception ex)
        {
            Log.WritLog("gv_RowCommand:", ex.ToString());
        }
    }
    // 手动输入
    protected void btninptuadd_Click(object sender, EventArgs e)
    {
        CommonMethod.IsTimeOutUserCode(this);
        bool flag = Check();
        if (flag)
        {
            AccountStatementItemModel model = new AccountStatementItemModel();
            model.AccountStatementCode = MCInfomationMBLL.GetAccountStatementCode();
            model = SetItemModel(model);
            flag = AccountStatementItemBLL.Add(model);

            if (flag)
            {
                Session["dtlist"] = null;
                InitDataBind();
                Clear();
                tc.ActiveTabIndex = 0;
                CommonMethod.regAlertMessage(udp, "添加成功！", this);
            }
            else
            {
                CommonMethod.regAlertMessage(udp, "添加失败！", this);
            }
        }
    }
    // 编辑
    protected void btnedit_Click(object sender, EventArgs e)
    {
        CommonMethod.IsTimeOutUserCode(this);
        if (!string.IsNullOrEmpty(hidAccountStatementItemCode.Value)
            && int.Parse(hidAccountStatementItemCode.Value.Trim()) > 0)
        {
            bool flag = Check();
            if (flag)
            {
                AccountStatementItemModel model = new AccountStatementItemModel();
                model.AccountStatementItemCode = hidAccountStatementItemCode.Value;
                model = SetItemModel(model);
                bool updateflag = AccountStatementItemBLL.Update(model);

                if (updateflag)
                {
                    hidAccountStatementItemCode.Value = "";
                    //CommonMethod.regAlertMessage(UpdatePanel, "保存成功！", this);
                    Session["dtlist"] = null;
                    InitDataBind();
                    btninptuadd.Visible = tc.TabPages[1].Visible ? true : false;
                    btnedit.Visible = false;
                    Clear();
                    tc.ActiveTabIndex = 0;
                }
                else
                    CommonMethod.regAlertMessage(udp, "保存失败！", this);
            }
        }
        else
            CommonMethod.regAlertMessage(udp, "数据获取失败，请从新开发！", this);
    }
    protected void btnreset_Click(object sender, EventArgs e)
    {
        btninptuadd.Visible = tc.TabPages[1].Visible ? true : false;
        btnedit.Visible = false;
        Clear();
    }
    private void Clear()
    {
        txtBankReference.Text = "";
        txtEntryDate.Text = System.DateTime.Now.ToString("yyyy-MM-dd");
        txtTransactionAmount.Text = "0";
        txtOrderingPartyName.Text = "";
        txtOrderingPartyBankName.Text = "";
        chkAcceted.Checked = false;
        txtCustomerReference.Text = "";
        txtValueDate.Text = System.DateTime.Now.ToString("yyyy-MM-dd");
        txtProductType.Text = "";
        txtCustomerCode.Text = "";
        txtCustomerName.Text = "";
        txtTransactionDescription.Text = "";
        txtExtraInformation.Text = "";
        txtOrderingPartyAccount.Text = "";
        txtPaymentDetails.Text = "";
        txtMOLine.Text = "";
        txtMOPerson.Text = "";
        txtMaturityDate.Text = System.DateTime.Now.ToString("yyyy-MM-dd");
        txtRemarks.Text = "";
    }

    #region 导出

    // 模版导入之数据导出
    protected void btnImportingExport_Click(object sender, EventArgs e)
    {
        TemplateDataBind();

        gvreadyforimport.Columns[0].Visible = false;
        gridready.WriteXlsToResponse();
        gvreadyforimport.Columns[0].Visible = true;
    }
    // 已入库之数据导出
    protected void btnImportedExport_Click(object sender, EventArgs e)
    {
        InitDataBind();

        gvimported.Columns[0].Visible = false;
        gvimported.Columns[1].Visible = false;
        gridreadied.WriteXlsToResponse();
        gvimported.Columns[0].Visible = true;
        gvimported.Columns[1].Visible = true;
    }

    #endregion

    public string GetSelectedAmount()
    {
        int rowindex = -1;
        List<object> rowindexList = gvreadyforimport.GetSelectedFieldValues("rowindex");
        if (rowindexList != null)
        {
            if (Session["listdt"] != null)
            {
                listdt = Session["listdt"] as List<DataTable>;
                DataTable tempdt = listdt[2];
                if (tempdt != null && tempdt.Rows.Count > 0)
                {
                    int rowcount = tempdt.Rows.Count;
                    for (int i = 0; i < rowindexList.Count; i++)
                    {
                        if (rowindexList[i] == null || rowindexList[i] == DBNull.Value) continue;
                        rowindex += (int)rowindexList[i];
                        if (rowindex < rowcount)
                        {
                            tempdt.Rows[rowindex]["HandlingFee"] = 1;
                        }
                    }
                }
            }
        }
        return "";
    }

    #region Checked

    // 已承兑
    public string GetAccepted(string rowindex)
    {
        return "ad_" + rowindex;
    }
    // 手续费
    public string GetHandlingFee(string rowindex)
    {
        return "hf_" + rowindex;
    }
    public Boolean GetVisible(string flag)
    {
        return "1".Equals(flag);
    }
    public string GetString(string flag)
    {
        if ("1".Equals(flag))
            return "是";
        return "否";
    }
    
    protected void chk_CheckedChanged_gv(object sender, EventArgs e)
    {
        try
        {
            ASPxCheckBox chk = sender as ASPxCheckBox;
            string id = chk.ToolTip;
            int f = chk.Checked ? 1 : 0;
            string[] list = id.Split('_');
            string actionType = list[0] == "ad" ? "accepted" : "handlingfee";
            bool flag = AccountStatementItemBLL.Update(list[1], actionType, f);
            if (flag)
            {
                //CommonMethod.regAlertMessage(udp, "设置成功！", this);
                Session["dtlist"] = null;
                InitDataBind();
            }
            else
            {
                CommonMethod.regAlertMessage(udp, "设置失败！", this);
            }
        }
        catch (Exception ex)
        {
            Log.WritLog("chk_CheckedChanged_gv:", ex.ToString());
        }
    }
    protected void chk_CheckedChanged(object sender, EventArgs e)
    {
        ASPxCheckBox chk = sender as ASPxCheckBox;
        string id = chk.ToolTip;
        if (Session["listdt"] != null)
        {
            listdt = Session["listdt"] as List<DataTable>;
            DataTable tempdt = listdt[2];
            if (tempdt != null && tempdt.Rows.Count > 0)
            {
                int rowcount = tempdt.Rows.Count;
                string[] list = id.Split('_');
                if (rowcount > 0 && list != null && list.Length > 1)
                {
                    try
                    {
                        int rowindex = int.Parse(list[1]);
                        if (rowcount > rowindex)
                        {
                            if ("hf".Equals(list[0]))
                            {
                                tempdt.Rows[rowindex - 1]["HandlingFee"] = chk.Checked ? 1 : 0;
                                tempdt.Rows[rowindex - 1]["HandlingFeeShowName"] = chk.Checked ? "是" : "否";
                            }
                            else if ("ad".Equals(list[0]))
                            {
                                tempdt.Rows[rowindex - 1]["Accepted"] = chk.Checked ? 1 : 0;
                                tempdt.Rows[rowindex - 1]["AcceptedShowName"] = chk.Checked ? "是" : "否";
                            }

                            listdt[2] = tempdt;
                            gvreadyforimport.DataSource = tempdt;
                            gvreadyforimport.DataBind();
                            Session["listdt"] = listdt;
                        }
                    }
                    catch (Exception ex)
                    {
                        Log.WritLog("chk_CheckedChanged:", ex.ToString());
                    }
                }
            }
        }
    }
    protected void CustomerNo_CheckedChanged(object sender, EventArgs e)
    {
        TextBox txt = sender as TextBox;
        string id = txt.ToolTip;
        bool success = false;
        if (Session["listdt"] != null)
        {
            listdt = Session["listdt"] as List<DataTable>;
            DataTable tempdt = listdt[2];
            if (tempdt != null && tempdt.Rows.Count > 0)
            {
                int rowcount = tempdt.Rows.Count;
                if (rowcount > 0)
                {
                    try
                    {
                        int rowindex = int.Parse(id);
                        if (rowcount > rowindex)
                        {
                            tempdt.Rows[rowindex - 1]["CustomerCode"] = txt.Text.Trim();
                            tempdt.Rows[rowindex - 1]["CustomerName"] = "";
                            listdt[2] = tempdt;
                            Session["listdt"] = listdt;
                            success = true;
                        }
                    }
                    catch (Exception ex)
                    {
                        Log.WritLog("CustomerNo_CheckedChanged:", ex.ToString());
                    }
                }
            }
            else
            {
                CommonMethod.regAlertMessage(udp, "数据过时，请重新操作！", this);
            }
        }
    }

    // 是否已经导出
    public bool Exported(string statevalue)
    {
        return statevalue == "import";
    }
    // 是否可删除
    public bool IsDelete(string statevalue, double AppliedAmount)
    {
        if (statevalue == "import" && AppliedAmount == 0)
            return true;
        return false;
    }

    #endregion

    protected void cbcompany_SelectedIndexChanged(object sender, EventArgs e)
    {
        Session["dtlist"] = null;
        InitDataBind();
    }
    protected void btnimportedpagesize_Click(object sender, EventArgs e)
    {
        gvimported.SettingsPager.PageSize = int.Parse(txtPageSize.Value);
        InitDataBind();
    }
    protected void btnPagesizeImporting_Click(object sender, EventArgs e)
    {
        gvreadyforimport.SettingsPager.PageSize = int.Parse(txtPageSize.Value);
        TemplateDataBind();
    }

    // 已承兑
    public string GetAccepted2(string rowindex)
    {
        return "ad_" + rowindex;
    }
    // 手续费
    public string GetHandlingFee2(string rowindex)
    {
        return "hf_" + rowindex;
    }
    public Boolean GetVisible2(string flag)
    {
        CommonMethod.regAlertMessage(udp, "数据过时，请重新操作！", this);
        return txtAccountStatementItemCodeHandlingFee.Value.Contains("hf_" + flag + ",");
    }
    public string GetString2(string flag)
    {
        if ("1".Equals(flag))
            return "是";
        return "否";
    }
    // 是否已经导出
    public bool Exported2(string statevalue)
    {
        return !(statevalue == "import");
    }
    // 是否可删除
    public bool IsDelete2(string statevalue, double AppliedAmount)
    {
        if (statevalue == "import" && AppliedAmount == 0)
            return true;
        return false;
    }
}